export default {
  data() {
    return {
      wgColorList: [], //选择配置后,通过选择配置带出外观颜色list
      nsColorList: [], //选择配置后,通过选择配置带出内试颜色list
      xedColorList: [], //选择后的外观颜色list
      xzwgcolor: {
        outerColour: {},
        inColour: {}
      }, //选择外观颜色
      colortype: true, //未选择颜色,出现的问题提示
      fanhuiimg: '', //相册上传图片后 返回的图片
      //表示所有颜色色全
      Sequanwg: {
        colorCoding: 'linear-gradient(0deg,rgba(255,49,49,1),rgba(255,243,52,1),rgba(37,191,255,1))',
        color: '色全'
      },
      Sequanns: {
        neiColor: ['linear-gradient(0deg,rgba(255,49,49,1),rgba(255,243,52,1),rgba(37,191,255,1))'],
        color: '色全',
        colorCoding: 'linear-gradient(0deg,rgba(255,49,49,1),rgba(255,243,52,1),rgba(37,191,255,1))',
      },
      colorLists: [],
      carType: '', //发布车源传入
      showPage: false
    }
  },
  onLoad(type) {
    //传入的颜色list
    this.colorLists = JSON.parse(decodeURIComponent(type.colorList))
    this.carType = type.type //判断车源传入
    if (this.carType === '1110') {
      this.xedColorList = JSON.parse(decodeURIComponent(type.xedColorLists))
      console.log(111, this.xedColorList)
    }
    //调取颜色接口
    this.GetCarColourList()
  },

  methods: {
    // 调取颜色接口
    async GetCarColourList() {
      try {
        let data = {
          'Id': this.colorLists.id
        }
        const res = await this.$api.common.GetCarColourList(data)
        this.wgColorList = res.data.outerColour //外观颜色
        this.wgColorList = [this.Sequanwg].concat(this.wgColorList) //色全
        this.nsColorList = this.filtercolor(res.data.inColour) //内试颜色
        this.nsColorList = [this.Sequanns].concat(this.nsColorList) //色全
        this.showPage = true

      } catch (err) {
        console.log(err)
      }
    },
    //分割颜色
    filtercolor(data) {
      // console.log(data)
      data.forEach(item => {
        var arr = item.colorCoding.split('/')
        item.neiColor = arr
      })
      return data
    },
    //颜色不可重复选择
    fliters(arr, obj) {
      var flag = false
      if (arr.length === 0) {
        return false
      }
      arr.forEach(item => {
        if (JSON.stringify(item) === JSON.stringify(obj)) {
          flag = true
        }
      })
      return flag
    },
    toTop() {
      uni.pageScrollTo({
        scrollTop: 0,
        duration: 100,
      })
    },
    //外观颜色选择
    wgcolorbtn(type) {
      var outerimg = {
        'colorCoding': type.colorCoding,
        'color': type.color
      }
      this.xzwgcolor.outerColour = outerimg
      if (this.xzwgcolor.inColour && this.xzwgcolor.outerColour.colorCoding && this.xzwgcolor.inColour.colorCoding) {
        this.colortype = false
        if (this.fliters(this.xedColorList, this.xzwgcolor)) {
          setTimeout(function() {
            uni.showToast({
              title: '请不要选择已有的颜色',
              duration: 2000,
              icon: 'none'
            })
          }, 100)
          this.xzwgcolor = {
            outerColour: {},
            inColour: {}
          }
          return
        }
        if (this.xedColorList.length > 0) {
          for (var i = 0; i < this.xedColorList.length; i++) {
            if (this.xedColorList[i].outerColour.color === '色全' && this.xedColorList[i].inColour.color === '色全') {
              setTimeout(function() {
                uni.showToast({
                  title: '色全已包含其他颜色',
                  duration: 2000,
                  icon: 'none'
                })
              }, 100)
              this.xzwgcolor = {
                outerColour: {},
                inColour: {}
              }
              return
            }
            if (this.xzwgcolor.inColour.color === '色全' && this.xzwgcolor.outerColour.color === '色全') {
              setTimeout(function() {
                uni.showToast({
                  title: '已包有其他颜色，不可再选色全',
                  duration: 2000,
                  icon: 'none'
                })
              }, 100)
              this.xzwgcolor = {
                outerColour: {},
                inColour: {}
              }
              return
            }
          }
        }
        if (this.xedColorList.length === 14) {
          setTimeout(function() {
            uni.showToast({
              title: '最多选择14个组合颜色',
              duration: 2000,
              icon: 'none'
            })
          }, 100)
          this.xzwgcolor = {
            outerColour: {},
            inColour: {}
          }
          return
        } else {
          this.toTop()
          this.xedColorList.push(this.xzwgcolor)
        }
        this.xzwgcolor = {
          outerColour: {},
          inColour: {}
        }
      }
    },
    //内试颜色选择
    nscolorbtn(type) {
      var inimg = {
        'colorCoding': type.colorCoding,
        'color': type.color,
        'neiColor': type.neiColor
      }
      this.xzwgcolor.inColour = inimg
      if (this.xzwgcolor.outerColour && this.xzwgcolor.outerColour.colorCoding && this.xzwgcolor.inColour.colorCoding) {
        this.colortype = false
        if (this.fliters(this.xedColorList, this.xzwgcolor)) {
          setTimeout(function() {
            uni.showToast({
              title: '请不要选择已有的颜色',
              duration: 2000,
              icon: 'none'
            })
          }, 100)
          this.xzwgcolor = {
            outerColour: {},
            inColour: {}
          }
          return
        }
        if (this.xedColorList.length > 0) {
          for (var i = 0; i < this.xedColorList.length; i++) {
            if (this.xedColorList[i].inColour.color === '色全' && this.xedColorList[i].outerColour.color === '色全') {
              setTimeout(function() {
                uni.showToast({
                  title: '色全已包含其他颜色',
                  duration: 2000,
                  icon: 'none'
                })
              }, 100)
              this.xzwgcolor = {
                outerColour: {},
                inColour: {}
              }
              return
            }
            if (this.xzwgcolor.inColour.title === '色全' && this.xzwgcolor.outerColour.title === '色全') {
              setTimeout(function() {
                uni.showToast({
                  title: '已包有其他颜色，不可再选色全',
                  duration: 2000,
                  icon: 'none'
                })
              }, 100)
              this.xzwgcolor = {
                outerColour: {},
                inColour: {}
              }
              return
            }
          }
        }
        if (this.xedColorList.length === 14) {
          setTimeout(function() {
            uni.showToast({
              title: '最多选择14个组合颜色',
              duration: 2000,
              icon: 'none'
            })
          }, 100)
          this.xzwgcolor = {
            outerColour: {},
            inColour: {}
          }
          return
        } else {
          this.toTop()
          this.xedColorList.push(this.xzwgcolor)
        }
        this.xzwgcolor = {
          outerColour: {},
          inColour: {}
        }
      }
    },
    //删除颜色
    tagClick(e, index) {
      this.xedColorList.splice(index, 1) //取消
      if (this.xedColorList === '' || this.xedColorList === undefined || this.xedColorList.length === 0) {
        this.colortype = true
      }
    },
    wgnescolor(type) {
      if (type === 1) {
        if (this.xzwgcolor.outerColour.color === undefined) {
          setTimeout(function() {
            uni.showToast({
              title: '请选择外观颜色',
              duration: 2000,
              icon: 'none'
            })
          }, 100)
          return false
        }
        if (this.xzwgcolor.inColour.color === undefined) {
          setTimeout(function() {
            uni.showToast({
              title: '请选择内饰颜色',
              duration: 2000,
              icon: 'none'
            })
          }, 100)
          return false
        }
      } else {
        //存储颜色
        uni.$emit('exteriorColor', {
          colorList: this.xedColorList[0]
        })
        // 发布车源
        if (this.carType === '1110') {
          uni.$emit('carSourceColor', {
            colors: this.xedColorList
          })
        }
        uni.navigateBack()
      }
    },
  }
}
